package xjtu.zd.server.normal.login.mail;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import xjtu.zd.server.common.Result;

import javax.annotation.Resource;
import java.util.Random;

@RestController
@RequestMapping("/normal/mail")
public class MailController {

    @Resource
    private MailService mailService;

    @RequestMapping("/getcheckcode")
    public Result getCheckCode(@RequestBody CheckCodeDto dto){

        String type = dto.getType();
        if(type==null){
            type = "";
        }
        String checkCode = String.valueOf(new Random().nextInt(899999) + 100000);
        String message = "您的"+type+"验证码为："+checkCode;

        try {
            mailService.sendSimpleMail(dto.getEmail(), type+"验证码", message);
        }catch (Exception e){
            return Result.fail(Result.ERR_CODE_SYS,type+"验证码发送失败："+e.getMessage());
        }

        return Result.success(type+"验证码发送成功！",checkCode);

    }
}
